package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.doman.pojo.PageResult;
import com.itheima.doman.Result;
import com.itheima.doman.pojo.Emp;
import com.itheima.doman.pojo.EmpQueryParam;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

/*    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, String name, Integer gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageResult<Emp> pageResult = empService.page(page, pageSize, name, gender, begin, end);
        log.info("查询员工信息, page={}, pageSize={},name={},gender={},begin={},end={}", page, pageSize, name, gender, begin, end);
        return Result.success(pageResult);
    }*/
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        log.info("查询员工信息, {}", empQueryParam);
        return Result.success(pageResult);
    }
    @Log
    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        log.info("添加员工, {}", emp);
        empService.addEmp(emp);
        return Result.success();
    }
    @GetMapping("/list")
    public Result findAll(){
        List<Emp> empList= empService.findAll();
        return Result.success(empList);
    }
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除员工, ids={}", ids);
        empService.delete(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询员工信息, id={}", id);
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工信息, {}", emp);
        empService.update(emp);
        return Result.success();
    }
}
